//
//  MCOrderListCell.swift
//  StoreManage
//
//  Created by MC on 2017/12/21.
//  Copyright © 2017年 MC. All rights reserved.
//

import UIKit

class MCAfterSaleListCell: UITableViewCell {
    
    
    var model : MCAfterSaleListModel = MCAfterSaleListModel() {
        didSet {
            
            // 头部
            orderLabel.text = "订单编号:" + model.afterSn
            switch model.status {
            case 1:
                statusLabel.text = "待处理"
                statusLabel.textColor = MCColor_16(colorStr: "FD8748")
            case 2:
                // isRefund = 1仅退款  2退货退款
                if model.isRefund == 1 {
                    statusLabel.text = "待退款"
                    statusLabel.textColor = UIColor.MCMain
                } else {
                    // 详情的时候判断 sendBack 0未回寄  1已回寄 2已入库
                    statusLabel.textColor = MCColor_16(colorStr: "FD8748")
                    if model.sendBack == 0 {  // 0未回寄
                        statusLabel.text = "未回寄"
                    } else if model.sendBack == 1 {  // 1已回寄
                        statusLabel.text = "已回寄"
                    } else {   // sendBack == 2 待退款
                        statusLabel.text = "待退款"
                        statusLabel.textColor = UIColor.MCMain
                    }
                }
            case 3:
                statusLabel.text = "已完成"
                statusLabel.textColor = UIColor.MCGray_middle
            case 4:
                statusLabel.text = "已拒绝"
                statusLabel.textColor = UIColor.MCRed
            default:
                break
            }
            
            
            // 尾部
            switch model.isRefund {
            case 1:
                statusButton.setTitle(" 仅退款", for: UIControlState.normal)
                statusButton.setImage(#imageLiteral(resourceName: "Order_backMoney"), for: UIControlState.normal)
            case 2:
                statusButton.setTitle(" 退货退款", for: UIControlState.normal)
                statusButton.setImage(#imageLiteral(resourceName: "Order_backGoods"), for: UIControlState.normal)
            default:
                break
            }
            
            
            if model.refundCashCoin > 0 {
                moneyLabel.text = MCString_judgemantDecimalsIsZero(figure: model.refundMoney) + "+" + "现金币" + String(model.refundCashCoin)
            } else {
                moneyLabel.text = MCString_judgemantDecimalsIsZero(figure: model.refundMoney)
            }
            
            // 商品
            iconImageView.sd_setImage(with: URL.init(string: model.goodImg), placeholderImage: #imageLiteral(resourceName: "Reuse_placeholder_1_1"))
            nameLabel.text = model.goodsName
            specLabel.text = model.goodSpecInfo
            numberLabel.text = "X" + String(model.quantity)
            saleMoneyLabel.text = MCString_judgemantDecimalsIsZero(figure: model.salePrice)
        }
    }

    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = UIColor.MCGray_light
        
        self.addSubview(bgView)
        bgView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(self).inset(UIEdgeInsetsMake(0, 0, 5, 0))
        }
        
        // 头部
        
        bgView.addSubview(orderImageView)
        orderImageView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(10)
            make.top.equalTo(bgView).offset(14)
            make.width.equalTo(12)
            make.height.equalTo(14)
        }
        
        bgView.addSubview(orderLabel)
        orderLabel.snp.updateConstraints { (make) -> Void in
            make.top.equalTo(bgView)
            make.left.equalTo(orderImageView.snp.right).offset(3)
            make.height.equalTo(40)
        }
        
        bgView.addSubview(statusLabel)
        statusLabel.snp.remakeConstraints { (make) ->Void in
            make.centerY.equalTo(orderLabel)
            make.right.equalTo(bgView).offset(-10)
        }

        
        // 商品
        bgView.addSubview(goodsView)
        goodsView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(self).inset(UIEdgeInsetsMake(40, 0, 45, 0))
        }
        
        goodsView.addSubview(iconImageView)
        iconImageView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(goodsView).offset(10)
            make.centerY.equalTo(goodsView)
            make.size.equalTo(CGSize.init(width: 80, height: 80))
        }
        
        goodsView.addSubview(numberLabel)
        numberLabel.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(goodsView).offset(-10)
            make.bottom.equalTo(iconImageView).offset(-2)
            make.width.equalTo(40)
            make.height.equalTo(20)
        }
        
        
        
        goodsView.addSubview(saleMoneyLabel)
        saleMoneyLabel.snp.remakeConstraints { (make) ->Void in
            make.centerY.equalTo(numberLabel)
            make.left.equalTo(iconImageView.snp.right).offset(10)
            make.height.equalTo(20)
        }
        
        
        goodsView.addSubview(specLabel)
        specLabel.snp.remakeConstraints { (make) ->Void in
            make.bottom.equalTo(saleMoneyLabel.snp.top).offset(-2)
            make.left.equalTo(iconImageView.snp.right).offset(10)
            make.right.equalTo(goodsView).offset(-10)
            make.height.equalTo(15)
        }

        
        
        
        
        goodsView.addSubview(nameLabel)
        nameLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(iconImageView.snp.right).offset(10)
            make.right.equalTo(goodsView).offset(-10)
            make.top.equalTo(iconImageView).offset(2)
            make.bottom.lessThanOrEqualTo(specLabel.snp.top).offset(-5)
        }
        
        // 尾部视图
        bgView.addSubview(moneyLabel)
        moneyLabel.snp.remakeConstraints { (make) ->Void in
            make.bottom.equalTo(bgView)
            make.right.equalTo(bgView).offset(-10)
            make.height.equalTo(40)
        }

        
        bgView.addSubview(statusButton)
        statusButton.snp.updateConstraints { (make) -> Void in
            make.centerY.equalTo(moneyLabel)
            make.left.equalTo(bgView).offset(10)
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    
    
    lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        return view
    }()
    
    lazy var goodsView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_light
        return view
    }()
    
    lazy var iconImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.layer.borderColor = UIColor.white.cgColor
        imageView.layer.borderWidth = 1
        imageView.image = #imageLiteral(resourceName: "Reuse_placeholder_1_1")
        return imageView
    }()

    
    
    public lazy var nameLabel: UILabel = {
        let label = UILabel.init()
        label.font = MCFont(14)
        label.textColor = UIColor.MCBlack
        label.numberOfLines = 0
        return label
    }()
    
    
    public lazy var specLabel: UILabel = {
        let label = UILabel.init()
        label.font = MCFont(12)
        label.textColor = UIColor.MCGray_middle
        label.text = "规格信息"
        return label
    }()
    
    public lazy var saleMoneyLabel: UILabel = {
        let label = UILabel.init()
        label.font = MCFont(13)
        label.textColor = UIColor.MCMain
        return label
    }()

    
    public lazy var numberLabel: UILabel = {
        let label = UILabel.init()
        label.font = MCFont(14)
        label.textColor = UIColor.MCBlack
        label.textAlignment = NSTextAlignment.right
        return label
    }()
    
    
    // 头部
    lazy var orderImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = #imageLiteral(resourceName: "Order_prompt")
        return imageView
    }()
    public lazy var orderLabel: UILabel = {
        let label = UILabel.init()
        label.font = MCFont(13)
        label.textColor = UIColor.MCGray_middle
        return label
    }()
    
    public lazy var statusLabel: UILabel = {
        let label = UILabel.init()
        label.font = MCFont(14)
        return label
    }()

    // 尾部
    public lazy var statusButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.titleLabel?.font = MCFont(14)
        button.setTitleColor(UIColor.black, for: UIControlState.normal)
        return button
    }()
    
    public lazy var moneyLabel: UILabel = {
        let label = UILabel.init()
        label.font = MCFont(12)
        label.textColor = UIColor.red
        return label
    }()
}


